######################################
# STC specific options
######################################
#MODEL = small
MODEL = medium
#MODEL = large
IRAM_SIZE =	256
XRAM_SIZE  =	1024
CODE_SIZE  =	8192

######################################
# target
######################################
TARGET = STC8G1K08

######################################
# building variables
######################################
# debug build?
DEBUG = 1
# optimization
#OPT = --opt-code-speed
OPT = --opt-code-size


#######################################
# paths
#######################################
# Build path
BUILD_DIR = build

######################################
# source
######################################
# C sources
C_SOURCES =  \
src/main.c \
drivers/prepherials/gpio/gpio.c \
drivers/prepherials/delay/delay.c \
drivers/prepherials/uart/uart.c

# ASM sources
ASM_SOURCES =

#######################################
# binaries
#######################################
PREFIX =# arm-none-eabi-
CC = sdcc
AS = $(GCC_PATH)/$(PREFIX)gcc -x assembler-with-cpp
CP = $(GCC_PATH)/$(PREFIX)objcopy
SZ = $(GCC_PATH)/$(PREFIX)size

#HEX = $(CP) -O ihex
HEX = packihx 
#BIN = $(CP) -O binary -S
BIN = makebin

#######################################
# CFLAGS
#######################################
# cpu
#CPU = -mcpu=cortex-m23

# fpu
# NONE for Cortex-M0/M0+/M3

# float-abi

# mcu
#MCU = $(CPU) -mthumb $(FPU) $(FLOAT-ABI)
MCU =

# macros for gcc
# AS defines
AS_DEFS = 

C_DEFS = 
#C_DEFS = -DSDCC
#C_DEFS = -D_STC8G_H

# AS includes
AS_INCLUDES = 


#C_INCLUDES =  \
#-Iheader \
#-ILib/gpio \
#-IInc
C_INCLUDES =  \
-I./ \
-Iinc \
-Idrivers/prepherials/gpio \
-Idrivers/prepherials/delay \
-Idrivers/prepherials/uart


# compile gcc flags
#ASFLAGS = $(MCU) $(AS_DEFS) $(AS_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections
ASFLAGS =
#CFLAGS = $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections
#CFLAGS = -mmcs51 $(C_DEFS) $(C_INCLUDES) $(OPT)
CFLAGS = -mmcs51 --model-$(MODEL) $(C_DEFS) $(C_INCLUDES) $(OPT) -L libsdcc.lib 

#######################################
# LDFLAGS
#######################################
# link script
#LDSCRIPT = GD32E230F6_FLASH.ld
LDSCRIPT =

# libraries
#LIBS = -lc -lm -lnosys 
#LIBDIR = 
#LDFLAGS = $(MCU) -specs=nano.specs -T$(LDSCRIPT) $(LIBDIR) $(LIBS) -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref -Wl,--gc-sections
LIBS =
LIBDIR =
#LDFLAGS =
#LDFLAGS = --model-$(MODEL) --stack-size $(IRAM_SIZE) --xram-size $(XRAM_SIZE) --code-size $(CODE_SIZE)
LDFLAGS = --model-$(MODEL) --iram-size $(IRAM_SIZE) --xram-size $(XRAM_SIZE) --code-size $(CODE_SIZE) 

# default action: build all
#all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin
all: $(BUILD_DIR)/$(TARGET).ihx $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin

#######################################
# build the application
#######################################
# list of objects
OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.rel)))
vpath %.c $(sort $(dir $(C_SOURCES)))
# list of ASM program objects
OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(ASM_SOURCES:.s=.rel)))
vpath %.s $(sort $(dir $(ASM_SOURCES)))

#$(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR) 
#	$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@
$(BUILD_DIR)/%.rel: %.c Makefile | $(BUILD_DIR) 
	$(CC)  $(CFLAGS) -c $< -o $@

#$(BUILD_DIR)/%.rel: %.s Makefile | $(BUILD_DIR)
#	$(AS) -c $(CFLAGS) $< -o $@

#$(BUILD_DIR)/$(TARGET).ihx: $(OBJECTS) Makefile
#	$(CC) $(OBJECTS) $(LDFLAGS) -o $@
#	$(SZ) $@
$(BUILD_DIR)/$(TARGET).ihx: $(OBJECTS) Makefile
	$(CC) $(OBJECTS) $(LDFLAGS) -o $@
#	$(SZ) $@

$(BUILD_DIR)/%.hex: $(BUILD_DIR)/%.ihx | $(BUILD_DIR)
	$(HEX) $< > $@

$(BUILD_DIR)/%.bin: $(BUILD_DIR)/%.ihx | $(BUILD_DIR)
	$(BIN) $< > $@
	
$(BUILD_DIR):
	mkdir $@		

#######################################
# clean up
#######################################
clean:
	rm -rf $(BUILD_DIR)
  
#######################################
# dependencies
#######################################
-include $(wildcard $(BUILD_DIR)/*.d)
